﻿using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;

namespace WindowsFormsApplication3
{
    /// <summary>
    /// Anyale 的摘要说明。
    /// </summary>
    public class Anyale
    {
        private string gAnyale_strMeterAddress = "AAAAAAAAAAAA";
        private CommProcess cp = new CommProcess();      //创建一个CommProcess对象
        private string PassWord = "42171561";            //超级密码
        private string strPASSWORD = "42171561";         //超级密码
        private string strDI1DI0 = "xxxx";               //97数据标识
        private string strDI3DI2DI1DI0 = "XXXXXXXX";     //07数据标识
        private string strOperator = "11223344";
        private string strDATA = "";

        #region Anyale类构造函数1
        public Anyale()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion

        #region Anyale类构造函数2
        public Anyale(string meterAddr)
        {
            gAnyale_strMeterAddress = meterAddr;
        }
        #endregion

        #region Anyale类构造函数3
        public Anyale(string meterAddr, int iPort, int iBaudRate, int iDataBit, int iStopBit, string strParity)    //默认构造函数
        {
            cp.gComm_iPort = iPort;
            cp.gComm_iBaudRate = iBaudRate;
            cp.gComm_iDataBit = iDataBit;
            cp.gComm_iStopBit = iStopBit;
            cp.gComm_strParity = strParity;
            cp.gComm_strMeterAddress = meterAddr;
        }
        #endregion

        #region 设置通讯组件串口通讯参数
        public void SetAnyaleCom(int iPort, int iBaudRate, int iDataBit, int iStopBit, string strParity, string meterAddr)    //默认构造函数
        {
            cp.gComm_iPort = iPort;          //设置端口
            cp.gComm_iBaudRate = iBaudRate;  //设置波特率
            cp.gComm_iDataBit = iDataBit;    //设置数据位
            cp.gComm_iStopBit = iStopBit;    //设置停止位
            cp.gComm_strParity = strParity;  //设置奇偶校验位
            cp.gComm_strMeterAddress = meterAddr;
        }
        #endregion

        #region 07协议读通讯地址
        public int DL645_07_13_READ_METER_ADDR(out string OutData)
        {
            int ret = 1;
            string strData = "";
            ret = cp.SendToMeter("13", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 进入厂内模式
        public int DL645_04_6A00()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "006A";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion


        public int DL645_15_ADRESS(string setAddress)
        {
            string OutData;
            int ret = 1;
            ret = cp.SendToMeter("15", setAddress, out OutData);
            return (ret);
        }
 

        #region 退出厂内模式
        public int DL645_04_6B00()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "006B";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 参数初始化 盲样初始化
        public int DL645_04_6A02()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "026A";
            strDATA = "02";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 参数初始化 盲样初始化
        public int DL645_04_6A02(string strXXXX)
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "026A";
            strDATA = "02";
            string strData = strDI1DI0 + strPASSWORD + strXXXX + strDATA;

            
            ret = cp.SendToMeter("04", strData, out OutData, 50);
            return (ret);
        }
        #endregion



        #region 参数初始化 参数初始化,不初始化通讯地址、生产日期、资产号
        public int DL645_04_6A02_01()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "026A";
            strDATA = "01";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 厂内清零
        public int DL645_04_6AFF()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "FF6A";
            strDATA = "";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 时间校时
        public int DL645_14_0400010C()
        {
            string OutData;
            int ret = 1;
            strDI3DI2DI1DI0 = "0C010004";
            strDATA = DL645DataTime();
            string strData = strDI3DI2DI1DI0 + strPASSWORD + strOperator + strDATA;
            ret = cp.SendToMeter("14", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 读内部程序版本号
        public int DL645_01_8000(out string OutData)
        {
            int ret = 1;
            strDI1DI0 = "0080";
            string strData = strDI1DI0;
            ret = cp.SendToMeter("01", strData, out OutData);
            //OutData = cp.HiToDi(OutData);
            return (ret);
        }
        #endregion


        #region 读取数据包
        public int DL645_01_6B01(out string OutData)
        {
            int ret = 1;
            strDI1DI0 = "016B";
            string strData = strDI1DI0;
            ret = cp.SendToMeter("01", strData, out OutData);
            //OutData = cp.HiToDi(OutData);
            return (ret);
        }
        #endregion


    


        //645数据
        public string DL645DataTime()
        {
            DateTime re = DateTime.Now;
            string strSecond = DateTime.Now.Second.ToString();
            if (strSecond.Length == 1)
            {
                strSecond = "0" + strSecond;
            }
            string strMinute = DateTime.Now.Minute.ToString();
            if (strMinute.Length == 1)
            {
                strMinute = "0" + strMinute;
            }
            string strHour = DateTime.Now.Hour.ToString();
            if (strHour.Length == 1)
            {
                strHour = "0" + strHour;
            }
            string strDay = DateTime.Now.Day.ToString();
            if (strDay.Length == 1)
            {
                strDay = "0" + strDay;
            }
            string weekstr = DateTime.Now.DayOfWeek.ToString();
            switch (weekstr)
            {
                case "Monday": weekstr = "01"; break;
                case "Tuesday": weekstr = "02"; break;
                case "Wednesday": weekstr = "03"; break;
                case "Thursday": weekstr = "04"; break;
                case "Friday": weekstr = "05"; break;
                case "Saturday": weekstr = "06"; break;
                case "Sunday": weekstr = "07"; break;
            }
            string strMonth = DateTime.Now.Month.ToString();
            if (strMonth.Length == 1)
            {
                strMonth = "0" + strMonth;
            }
            string strtime = strSecond + strMinute + strHour + weekstr + strDay + strMonth + DateTime.Now.Year.ToString().Substring(2, 2);

            return (strtime);
        }

        #region 校表初始化
        public int DL645_04_6A01_35(string strHFconst, string PQStart, string strMC, string strU, string strIB)
        {
            string strCode = "23";  //(1)
            string EMUCFG  = "00";  //(1)
            string ANAEN   = "00";  //(1)
            string ADCCON  = "00";  //(1)
            string CHK     = "00";  //(1)
            string I2Gain  = "0000";  //

            string OutData;
            int ret = 1;
            strDI1DI0 = "016A";

            int iMc = Convert.ToInt32(strMC);
            strMC = iMc.ToString("X4");

            double dl_U = Convert.ToDouble(strU) * 1000;
            Int32 iU = Convert.ToInt32(dl_U);

            strU = iU.ToString("X6");

            double dl_Ib = Convert.ToDouble(strIB) * 0.05 * 1000;
            Int32 iIb = Convert.ToInt32(dl_Ib);
            strIB = iIb.ToString("X4");

            strHFconst = StringProcess.DataFormat(strHFconst, 4);
            PQStart = StringProcess.DataFormat(PQStart,    4);
            strMC = StringProcess.DataFormat(strMC,      4);
            strU = StringProcess.DataFormat(strU,       6);
            strIB = StringProcess.DataFormat(strIB,      4);


            strDATA = strCode + EMUCFG + ANAEN + ADCCON + CHK + strHFconst + PQStart + I2Gain + strMC + strU + strIB;
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 单点快速校表 第1路 Un Ib 0.5L
        public int DL645_04_6A01_21(string strU, string strI, string strP)
        {
            string strCode     = "21";
            string strAdj5Ib   = "0000";  //5%Ib 
            string strAdj10Ib  = "00";    
            string strAdj05LIb = "00";
            double dl_U = Convert.ToDouble(strU);          //U
            double dl_I = Convert.ToDouble(strI) * 1000;   //mA
            double dl_P = Convert.ToDouble(strP) * 1000;   //mW
            double dl_S = dl_U * dl_I;                     //V*mA
            dl_U *= 1000;

            int iU = Convert.ToInt32(dl_U);
            int iI = Convert.ToInt32(dl_I);
            int iP = Convert.ToInt32(dl_P);
            int iS = Convert.ToInt32(dl_S);

            string strHexU = iU.ToString("X6");
            string strHexI = iI.ToString("X6");
            string strHexP = iP.ToString("X8");
            string strHexS = iS.ToString("X8");

            strHexU = StringProcess.DataFormat(strHexU, strHexU.Length);
            strHexI = StringProcess.DataFormat(strHexI, strHexI.Length);
            strHexP = StringProcess.DataFormat(strHexP, strHexP.Length);
            strHexS = StringProcess.DataFormat(strHexS, strHexS.Length);
            string OutData;
            int ret = 1;
            strDI1DI0 = "016A";
            strCode = "21";

            strDATA = strCode + strHexI + strHexP + strHexS + strAdj5Ib + strHexU + strAdj10Ib + strAdj05LIb;
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 单点快速校表 第2路 Un Ib 0.5L
        public int DL645_04_6A01_22(string strI, string strP)
        {
            double dl_I = Convert.ToDouble(strI) * 1000;   //mA
            double dl_P = Convert.ToDouble(strP) * 1000;   //mW
            int iI = Convert.ToInt32(dl_I);
            int iP = Convert.ToInt32(dl_P);
            string strHexI = iI.ToString("X6");
            string strHexP = iP.ToString("X8");
            strHexI = StringProcess.DataFormat(strHexI, strHexI.Length);
            strHexP = StringProcess.DataFormat(strHexP, strHexP.Length);
            string OutData;
            int ret = 1;
            strDI1DI0 = "016A";
            string strCode = "22";
            strDATA = strCode + strHexI + strHexP;
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 读取计量芯片寄存器值
        public int DL645_01_7001(string strRegAddr, out string strRegValue)
        {
            if(strRegAddr.Length == 1)
            {
                strRegAddr = "0" + strRegAddr;
            }

            int ret = 1;
            strDI1DI0 = "7001";
            strDI1DI0 = StringProcess.DataFormat(strDI1DI0, strDI1DI0.Length);
            strDATA = strDI1DI0 + strRegAddr;
            ret = cp.SendToMeter("01", strDATA, out strRegValue);  //OutData寄存器地址
            strRegValue = StringProcess.DataFormat(strRegValue, strRegValue.Length);
            return (ret);
        }
        #endregion


        #region 设置计量芯片寄存器值
        public int DL645_04_7001_09(string strRegAddr, string strValue)
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "7001";
            string strCode = "09";
            strDI1DI0 = StringProcess.DataFormat(strDI1DI0, strDI1DI0.Length);
            strValue = StringProcess.DataFormat(strValue, 6);
            string strData = strDI1DI0 + strPASSWORD + strCode + strValue + strRegAddr; ;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 设置电压影响量寄存器修正值
        public int DL645_04_7001_0B(int strUnSelect, string strValue)
        {
            string OutData;
            string strSelect;
            int ret = 1;
            strDI1DI0 = "7001";
            string strCode = "0B";
            strSelect = strUnSelect.ToString("X2");
            strValue = StringProcess.DataFormat(strValue, 4);
            strDI1DI0 = StringProcess.DataFormat(strDI1DI0, strDI1DI0.Length);

            string strData = strDI1DI0 + strPASSWORD +   strCode + strSelect + strValue;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 读取电压影响量寄存器修正值
        public int DL645_02_EE01640C(int strUnSelect, out string strValue)
        {
            string OutData;
            string strSelect;
            int ret = 1;
            string strData = "0C6401EE";      //数据标志
            ret = cp.SendToMeter("02", strData, out OutData);
            strValue = OutData.Substring((strUnSelect-1) *6, 4);
            strValue = StringProcess.DataFormat(strValue, strValue.Length);
            return (ret);
        }
        #endregion

        #region 读取电压影响量寄存器修正值
        public int DL645_02_EE014040(out string OutData)
        {
            int ret = 1;
            string strData = "404001EE";      //数据标志
            ret = cp.SendToMeter("02", strData, out OutData);
            return (ret);
        }
        #endregion



        #region 禁止明文红外
        public int DL645_04_6A06_OFF()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "066A";
            strDATA = "1234";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion

        #region 允许明文红外
        public int DL645_04_6A06_ON()
        {
            string OutData;
            int ret = 1;
            strDI1DI0 = "066A";
            strDATA = "FFFF";
            string strData = strDI1DI0 + strPASSWORD + strDATA;
            ret = cp.SendToMeter("04", strData, out OutData);
            return (ret);
        }
        #endregion
    }
}
